package com.payment.web.controller.app.notify;

import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.payment.common.base.AjaxResult;
import com.payment.system.domain.SystemConfig;
import com.payment.system.domain.alipay.AliNotifyReqEntity;
import com.payment.system.mapper.SystemConfigMapper;
import com.payment.system.service.IWxAliPayService;

/**
 * @author fuzhili
 * @Description: 微信支付宝 异步回调
 * @date 2019/2/16 9:40
 */
@RestController
@RequestMapping("/app/payNotify")
public class WxAliPayNotifyController {
	@Autowired
	public IWxAliPayService wxAliPayService;
	@Autowired
	private SystemConfigMapper configMapper;
	private static final Logger logger = LoggerFactory.getLogger(WxAliPayNotifyController.class);

	/**
	 * 微信异步通知回调接口
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("wxNotify")
	public void wxNotify(HttpServletRequest request, HttpServletResponse response) {
		logger.info("微信异步通知开始");
		String inputLine;
		String notityXml = "";
		String result;
		PrintWriter out = null;
		// 微信给返回的东西
		try {
			while ((inputLine = request.getReader().readLine()) != null) {
				notityXml += inputLine;
			}
			request.setCharacterEncoding("UTF-8");
			out = response.getWriter();
			out.write(this.setXml("SUCCESS", "OK"));
			out.flush();
			logger.info("微信给返回:====" + notityXml);
			boolean r = wxAliPayService.wxNotify(notityXml);
			logger.info("微信回调修改订单状态:====" + r);
			result = this.setXml("SUCCESS", "OK");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
			result = e1.toString();
		} catch (Exception e) {
			e.printStackTrace();
			result = e.toString();
		} finally {
			if (out != null) {
				out.close();
				out = null;
			}
		}
		logger.info(result);
		logger.info("微信异步通知结束");

	}

	/**
	 * 支付宝异步通知回调接口
	 *
	 * @param aliNotifyReqEntity
	 * @param resp
	 */
	@RequestMapping("aliNotifyTest")
	public AjaxResult aliNotifyTest(AliNotifyReqEntity aliNotifyReqEntity, HttpServletResponse resp) {
		System.out.println("支付宝回调了哦");
		SystemConfig s =	configMapper.selectSystemConfigByConfigCode("uoLevel");
		return AjaxResult.successAndData("成功",JSONArray.parse(s.getConfigValue()));
	}
	
	
	
	/**
	 * 支付宝异步通知回调接口
	 *
	 * @param aliNotifyReqEntity
	 * @param resp
	 */
	@RequestMapping("aliNotify")
	public void aliNotify(AliNotifyReqEntity aliNotifyReqEntity, HttpServletResponse resp) {
		logger.info("支付宝异步通知开始");
		logger.info(JSON.toJSONString(aliNotifyReqEntity));
		PrintWriter wt = null;
		String result = "";
		try {
			resp.reset();
			resp.setContentType("application/json");
			resp.setCharacterEncoding("utf-8");
			wt = resp.getWriter();
			result = wxAliPayService.aliNotify(aliNotifyReqEntity);
			wt.write(result);
			wt.flush();
			logger.info("支付宝异步===" + result);
		} catch (Exception e) {
			e.printStackTrace();
			logger.debug(e.toString());
		} finally {
			if (wt != null) {
				wt.close();
				wt = null;
			}
		}
		logger.info("支付宝异步通知结束");
	}

	private String setXml(String return_code, String return_msg) {

		return "<xml><return_code><![CDATA[" + return_code + "]]>" + "</return_code><return_msg><![CDATA[" + return_msg
				+ "]]></return_msg></xml>";
	}

}
